package org.hongmeng.service.impl;

import org.hongmeng.entity.Meeting;
import org.hongmeng.entity.MeetingParticipant;
import org.hongmeng.mapper.MeetingMapper;
import org.hongmeng.mapper.MeetingParticipantMapper;
import org.hongmeng.service.MeetingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class MeetingServiceImpl implements MeetingService {
    private final MeetingMapper meetingMapper;
    private final MeetingParticipantMapper participantMapper;

    public MeetingServiceImpl(MeetingMapper meetingMapper, MeetingParticipantMapper participantMapper) {
        this.meetingMapper = meetingMapper;
        this.participantMapper = participantMapper;
    }

    @Override
    @Transactional
    public Meeting createMeeting(Meeting meeting) {
        // 生成会议号
        meeting.setMeetingNo(generateMeetingNo());
        
        // 设置初始状态
        meeting.setStatus(0);
        meeting.setParticipantCount(0);
        
        // 检查会议时间
        validateMeetingTime(meeting);
        
        // 创建会议
        meetingMapper.insert(meeting);
        
        return meeting;
    }

    @Override
    public Meeting getMeetingByNo(String meetingNo) {
        return meetingMapper.selectByMeetingNo(meetingNo);
    }

    @Override
    public List<Meeting> getMeetingsByCreator(Long creatorId) {
        return meetingMapper.selectByCreatorId(creatorId);
    }

    @Override
    @Transactional
    public void updateMeeting(Meeting meeting) {
        validateMeetingTime(meeting);
        meetingMapper.updateById(meeting);
    }

    @Override
    @Transactional
    public void joinMeeting(String meetingNo, Long userId) {
        Meeting meeting = meetingMapper.selectByMeetingNo(meetingNo);
        if (meeting == null) {
            throw new RuntimeException("会议不存在");
        }

        // 检查会议状态
        LocalDateTime now = LocalDateTime.now();
        if (now.isBefore(meeting.getStartTime())) {
            throw new RuntimeException("会议还未开始");
        }
        if (now.isAfter(meeting.getEndTime())) {
            throw new RuntimeException("会议已结束");
        }

        // 检查是否已经加入
        MeetingParticipant participant = participantMapper.selectByMeetingIdAndUserId(meeting.getId(), userId);
        if (participant != null && participant.getStatus() == 1) {
            throw new RuntimeException("您已在会议中");
        }

        // 加入会议
        if (participant == null) {
            participant = new MeetingParticipant();
            participant.setMeetingId(meeting.getId());
            participant.setUserId(userId);
            participantMapper.insert(participant);
        } else {
            participantMapper.updateStatus(participant.getId(), 1);
        }

        // 更新会议参与人数
        meetingMapper.updateParticipantCount(meeting.getId(), meeting.getParticipantCount() + 1);
    }

    @Override
    @Transactional
    public void leaveMeeting(String meetingNo, Long userId) {
        Meeting meeting = meetingMapper.selectByMeetingNo(meetingNo);
        if (meeting == null) {
            throw new RuntimeException("会议不存在");
        }

        // 检查是否在会议中
        MeetingParticipant participant = participantMapper.selectByMeetingIdAndUserId(meeting.getId(), userId);
        if (participant == null || participant.getStatus() == 0) {
            throw new RuntimeException("您不在会议中");
        }

        // 离开会议
        participantMapper.updateLeaveTime(participant.getId());

        // 更新会议参与人数
        meetingMapper.updateParticipantCount(meeting.getId(), meeting.getParticipantCount() - 1);
    }

    @Override
    public List<MeetingParticipant> getMeetingParticipants(Long meetingId) {
        return participantMapper.selectByMeetingId(meetingId);
    }

    @Override
    public List<Meeting> getJoinedMeetings(Long userId) {
        List<MeetingParticipant> participants = participantMapper.selectByUserId(userId);
        List<Meeting> meetings = new ArrayList<>();
        for (MeetingParticipant participant : participants) {
            Meeting meeting = meetingMapper.selectById(participant.getMeetingId());
            if (meeting != null) {
                meetings.add(meeting);
            }
        }
        return meetings;
    }

    private String generateMeetingNo() {
        return UUID.randomUUID().toString().substring(0, 8);
    }

    private void validateMeetingTime(Meeting meeting) {
        if (meeting.getStartTime().isAfter(meeting.getEndTime())) {
            throw new RuntimeException("会议开始时间不能晚于结束时间");
        }
        if (meeting.getStartTime().isBefore(LocalDateTime.now())) {
            throw new RuntimeException("会议开始时间不能早于当前时间");
        }
    }
} 